package com.xiaogao.serviceEdu.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaogao.commonUtils.Result;
import com.xiaogao.serviceEdu.entity.EduCourse;
import com.xiaogao.serviceEdu.entity.vo.CourseInfoVO;
import com.xiaogao.serviceEdu.entity.vo.CoursePublishVo;
import com.xiaogao.serviceEdu.entity.vo.CourseQuery;
import com.xiaogao.serviceEdu.entity.vo.CourseWebVo;
import com.xiaogao.serviceEdu.service.EduChapterService;
import com.xiaogao.serviceEdu.service.EduCourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author 小高
 * @since 2021-06-08
 */
@Api(description = "课程管理")
@RestController
@RequestMapping("/serviceEdu/course")
public class EduCourseController {

    @Autowired
    private EduCourseService courseService;


    @ApiOperation(value = "添加课程")
    @PostMapping("/addCourseInfo")
    public Result addCourseInfo(
            @ApiParam(name = "courseInfoVO", value = "课程基本信息", required = true)
            @RequestBody CourseInfoVO courseInfoVO) {

        String courseId = courseService.saveCourseInfo(courseInfoVO);

        if (!StringUtils.isEmpty(courseId)){
            return Result.ok().data("courseId",courseId);
        }else{
            return Result.error().message("保存失败");
        }
    }

    @ApiOperation(value = "根据id查询课程信息")
    @GetMapping("/courseInfo/{id}")
    public Result getCourseInfoById(
            @ApiParam(name = "id",value = "课程ID",required = true)
            @PathVariable String id){

        CourseInfoVO courseInfoVO = courseService.getCourseInfoVoById(id);
        if (!StringUtils.isEmpty(courseInfoVO)){
            return Result.ok().data("item",courseInfoVO);
        }else{
            return Result.error();
        }

    }

    @ApiOperation(value = "根据id修改课程信息")
    @PutMapping("/updateCourseInfo/{id}")
    public Result updateCourseInfoById(
            @ApiParam(name = "courseInfoVO",value = "课程基本信息",required = true)
            @RequestBody CourseInfoVO courseInfoVO,

            @ApiParam(name = "id",value = "课程id",required = true)
            @PathVariable String id){

//        courseInfoVO.setId(id);
        String courseId = courseService.updateCourseInfoById(courseInfoVO);
        if (!StringUtils.isEmpty(courseId)){
            return Result.ok().data("courseId",courseId);
        }else{
            return Result.error();
        }
    }

    @ApiOperation(value = "根据id获取课程最终发布确认信息")
    @GetMapping("/coursePublishVo/{id}")
    public Result getCoursePublishVoById(
            @ApiParam(name = "id",value = "课程id",required = true)
            @PathVariable String id){

        CoursePublishVo coursePublishVo = courseService.getCoursePublishVoById(id);
        return Result.ok().data("item",coursePublishVo);
    }

    @ApiOperation(value = "根据id发布课程")
    @GetMapping("/publishCourseById/{id}")
    public Result publishCourseById(
            @ApiParam(name = "id",value = "课程id",required = true)
            @PathVariable String id){

        boolean flag = courseService.publishCourseById(id);
        return flag?Result.ok():Result.error();
    }

    @ApiOperation(value = "条件分页课程列表")
    @GetMapping("/pageCourseQuery/{page}/{limit}")
    public Result pageCourseQuery(
            @ApiParam(name = "page",value = "当前页数",required = true)
            @PathVariable Long page,

            @ApiParam(name = "limit",value = "每页显示条数",required = true)
            @PathVariable Long limit,

            @ApiParam(name = "courseQuery",value = "查询对象",required = false)
            CourseQuery courseQuery){

        Page<EduCourse> pageParam = new Page<>(page,limit);

        courseService.pageQuery(pageParam,courseQuery);
        // 分页查询得到每页集合数据
        List<EduCourse> courseList = pageParam.getRecords();
        // 分页查询得到每页条数
        long total = pageParam.getTotal();
        return Result.ok().data("total",total).data("rows",courseList);
    }

    @ApiOperation(value = "根据ID删除课程")
    @DeleteMapping("/removeCourse/{id}")
    public Result removeCourse(
            @ApiParam(name = "id",value = "课程id",required = true)
            @PathVariable String id){

            boolean flag = courseService.removeCourseById(id);
        if (flag){
            return Result.ok();
        }else{
            return Result.error().message("删除失败！");
        }
    }

}

